package com.evernote.controller;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.evernote.entity.Content;
import com.evernote.entity.NoteBook;
import com.evernote.service.ContentService;
import com.evernote.service.NoteBookService;

/**
 * Servlet implementation class DoAddNote
 */
@WebServlet("/DoAddNote")
public class DoAddNote extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoAddNote() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		
		String msg = "";
		
		int Cid = Integer.parseInt(request.getParameter("id"));
		String title = request.getParameter("title");
		String date = request.getParameter("date");
		String content = request.getParameter("content");
		int Nid = Integer.parseInt(request.getParameter("Nid"));
		int Uid = Integer.parseInt(request.getParameter("Uid"));
		
		if(Cid == 0) {
			
			NoteBook n = new NoteBook();
			NoteBookService nbs = new NoteBookService();
			Content c = new Content();
			ContentService cs = new ContentService();
			
			c.setTitle(title);
			c.setDate(date);
			c.setContent(content);
			c.setNid(Nid);
			c.setUid(Uid);
			
			Cid = cs.addNote(c);
			if(Cid != 0) {
				msg = "提交成功";
				
				int selectedNid = Nid;
				request.setAttribute("selectedNid", selectedNid);
				
				request.setAttribute("Cid", Cid);
				request.setAttribute("Uid", Uid);
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("ToIndex").forward(request, response);
			}
		}else {
			request.getRequestDispatcher("DoUpdateNote").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
